package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.ShowSearchCommoditiesCallback;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.domain.HistorySearch;
import cn.hrlly.tao2shou.listener.OnShowSearchCommoditiesListener;
import cn.hrlly.tao2shou.utils.DatabaseUtil;

/**
 * User: gengliang
 * Date: 2016-01-29
 * Time: 13:51
 */

public class ShowSearchCommoditiesBizImpl implements ShowSearchCommoditiesBiz {

    private OnShowSearchCommoditiesListener onShowSearchCommoditiesListener;
    private DatabaseUtil databaseUtil;

    public ShowSearchCommoditiesBizImpl(OnShowSearchCommoditiesListener onShowSearchCommoditiesListener,Context context){
        this.onShowSearchCommoditiesListener=onShowSearchCommoditiesListener;
        this.databaseUtil=new DatabaseUtil(context);
    }

    @Override
    public void showSearchCommodities(CommodityList commodityList) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action,Config.action_search_commodities)
                .addParams(Config.key_antistop,commodityList.getAntistop())
                .addParams(Config.key_page, String.valueOf(commodityList.getPage()))
                .addParams(Config.key_page_count, String.valueOf(commodityList.getPageCount()))
                .addParams(Config.key_sort_according, String.valueOf(commodityList.getSortAccording()))
                .addParams(Config.key_longditude, String.valueOf(commodityList.getLongitude()))
                .addParams(Config.key_lantitude, String.valueOf(commodityList.getLantitude()))
                .build()
                .execute(new ShowSearchCommoditiesCallback(onShowSearchCommoditiesListener));
    }

    @Override
    public void saveHistorySearch(HistorySearch historySearch) {
        databaseUtil.insertHistorySearch(historySearch);
    }

    @Override
    public void closeDataBase() {
        databaseUtil.closeDatabase();
    }
}
